﻿@page "/So/SaleReturn/Details"
@using nERP.ViewModel.So.SaleReturnVMs
@using nERP.Model.So
@using System.ComponentModel.DataAnnotations
@inherits BasePage

<div>
    <ValidateForm @ref="vform" Model="@Model" OnValidSubmit="@Submit">
        <Tab IsBorderCard="true">
            <TabItem Text="@WtmBlazor.Localizer["_Admin.BasicInfo"]">
                <Row ItemsPerRow="ItemsPerRow.Three" RowType="RowType.Normal">
                    <Display @bind-Value="@Model.Entity.SaleReturnCode" ShowLabel="true" />
                    <Display @bind-Value="@Model.Entity.SaleOutWarehouseId" ShowLabel="true" Lookup="AllSaleOutWarehouses" />
                    <Display @bind-Value="@Model.Entity.SaleId" ShowLabel="true" Lookup="AllSales" />
                    <Display @bind-Value="@Model.Entity.CustomerId" ShowLabel="true" Lookup="AllCustomers" />
                    <Display @bind-Value="@Model.Entity.InvoicesDate" ShowLabel="true" FormatString="yyyy-MM-dd" />
                    <Display @bind-Value="@Model.Entity.MakeUserId" ShowLabel="true" Lookup="AllMakeUsers" />
                    <Display @bind-Value="@Model.Entity.Status" ShowLabel="true" />
                    <Display @bind-Value="@Model.Entity.Remark" ShowLabel="true" />
                    <Display @bind-Value="@Model.Entity.TotalPrice" ShowLabel="true" FormatString="0.##" />
                    <Display @bind-Value="@Model.Entity.TotalPrice1" ShowLabel="true"  FormatString="0.##" />
                    <Display @bind-Value="@Model.Entity.TotalPrice2" ShowLabel="true" FormatString="0.##" />
                    <Display @bind-Value="@Model.Entity.CreateTime" ShowLabel="true" FormatString="yyyy-MM-dd HH:mm:ss" />
                    <Display @bind-Value="@Model.Entity.UpdateTime" ShowLabel="true" FormatString="yyyy-MM-dd HH:mm:ss" />
                    <Display @bind-Value="@Model.Entity.CreateBy" ShowLabel="true" />
                    <Display @bind-Value="@Model.Entity.UpdateBy" ShowLabel="true" />
                    <WTUploadImage @bind-Value="@Model.Entity.InvoicesFile" thumb-width="128" />
                </Row>
                <Divider Icon="fa-fw fa-solid fa-bookmark" Text="@WtmBlazor.Localizer["_Model._SaleReturn._SaleReturnDetail_SaleReturn"]" Alignment="Alignment.Left" />


                <Table TItem="SaleReturnDetail"
                       IsPagination="false"
                       IsStriped="true" IsBordered="true" IsMultipleSelect="false" @bind-Items="@SaleReturnDetailSaleReturnList3" ShowFooter="true"
                       ShowToolbar="false" ShowExtendButtons="false" ShowSkeleton="true">
                    <TableColumns>
                        <TableColumn @bind-Field="@context.Seq" Text="@WtmBlazor.Localizer["Page.序号"]" />
                        <TableColumn @bind-Field="@context.SaleReturnDetailCode" Text="@WtmBlazor.Localizer["Page.退货明细单号"]" Visible="false" />
                        <TableColumn @bind-Field="@context.SaleOutWarehouseDetailId" Text="@WtmBlazor.Localizer["Page.销售出库明细"]" Visible="false" />
                        <TableColumn @bind-Field="@context.SaleId" Text="@WtmBlazor.Localizer["Page.销售订单"]" Visible="false" />
                        <TableColumn @bind-Field="@context.SaleDetailId" Text="@WtmBlazor.Localizer["Page.销售明细"]" Visible="false" />
                        <TableColumn @bind-Field="@context.GoodsId" Text="@WtmBlazor.Localizer["Page.商品编号"]" />
                        <TableColumn @bind-Field="@context.GoodsName" Text="@WtmBlazor.Localizer["Page.商品名称"]" />
                        <TableColumn @bind-Field="@context.GoodsSpec" Text="@WtmBlazor.Localizer["Page.商品规格"]" />
                        <TableColumn @bind-Field="@context.GoodsUnit" Text="@WtmBlazor.Localizer["Page.计量单位"]" />
                        <TableColumn @bind-Field="@context.ReturnNumber" Text="@WtmBlazor.Localizer["Page.退货数量"]" FormatString="0.##" />
                        <TableColumn @bind-Field="@context.UnitPrice" Text="@WtmBlazor.Localizer["Page.退货单价"]" FormatString="0.##"  />
                        <TableColumn @bind-Field="@context.TotalPrice" Text="@WtmBlazor.Localizer["Page.退货金额"]" FormatString="0.##"  />
                        <TableColumn @bind-Field="@context.DiscountPrice" Text="@WtmBlazor.Localizer["Page.折扣金额"]"  FormatString="0.##" />
                        <TableColumn @bind-Field="@context.TaxRate" Text="@WtmBlazor.Localizer["Page.税率"]"  FormatString="0.##" />
                        <TableColumn @bind-Field="@context.TaxUnitPrice" Text="@WtmBlazor.Localizer["Page.含税单价"]" FormatString="0.##" />
                        <TableColumn @bind-Field="@context.TaxPrice" Text="@WtmBlazor.Localizer["Page.税额"]"  FormatString="0.##" />
                        <TableColumn @bind-Field="@context.TotalTaxPrice" Text="@WtmBlazor.Localizer["Page.含税金额"]" FormatString="0.##" />
                        <TableColumn @bind-Field="@context.Remark" Text="@WtmBlazor.Localizer["_Admin.Remark"]" />
                        <TableColumn @bind-Field="@context.CreateTime" Text="@WtmBlazor.Localizer["_Admin.CreateTime"]" FormatString="yyyy-MM-dd HH:mm:ss" />
                        <TableColumn @bind-Field="@context.UpdateTime" Text="@WtmBlazor.Localizer["_Admin.UpdateTime"]" FormatString="yyyy-MM-dd HH:mm:ss" />
                        <TableColumn @bind-Field="@context.CreateBy" Text="@WtmBlazor.Localizer["_Admin.CreateBy"]" />
                        <TableColumn @bind-Field="@context.UpdateBy" Text="@WtmBlazor.Localizer["_Admin.UpdateBy"]" />
                    </TableColumns>

                    <FooterTemplate>
                        <tr style="background-color:gray; font-weight:600; text-align:center;">
                            <td colspan="5">合计：</td>
                            <td>@sumReturnNumber</td>
                            <td colspan="1" />
                            <td>@sumTotalPrice</td>
                            <td style="color:red;">@sumDiscountPrice</td>
                            <td colspan="2" />
                            <td> @sumTaxPrice</td>
                            <td> @sumTotalTaxPrice</td>
                        </tr>
                    </FooterTemplate>

                </Table>

            </TabItem>

            <TabItem Text="@WtmBlazor.Localizer["Page.审批信息"]">
                <WTApproveHistory Model="@Model" />
            </TabItem>

        </Tab>


        <div class="modal-footer table-modal-footer">
            <Button OnClick="OnClose" Text="@WtmBlazor.Localizer["Sys.Close"]" />
        </div>

    </ValidateForm>

</div>



@code {
    [Parameter]
    public string id { get; set; }
    [Parameter]
    public string[] ids { get; set; }
    private SaleReturnVM Model = new SaleReturnVM();
    private ValidateForm vform { get; set; }
    private string sumReturnNumber { get; set; }
    private string sumTotalPrice { get; set; }
    private string sumDiscountPrice { get; set; }
    private string sumTaxPrice { get; set; }
    private string sumTotalTaxPrice { get; set; }

    private async Task Submit(EditContext context)
    {


        await PostsForm(vform, "/api/So/SaleReturn/Edit", (s) => "Sys.OprationSuccess");
    }
    public void OnClose()
    {
        CloseDialog();
    }

    private List<SelectedItem> AllSaleOutWarehouses = new List<SelectedItem>();
    private List<SelectedItem> AllSales = new List<SelectedItem>();
    private List<SelectedItem> AllCustomers = new List<SelectedItem>();
    private List<SelectedItem> AllMakeUsers = new List<SelectedItem>();
    private IEnumerable<SaleReturnDetail> SaleReturnDetailSaleReturnList3 { get; set; } = new List<SaleReturnDetail>();
    protected override async Task OnInitializedAsync()
    {


        AllSaleOutWarehouses = await WtmBlazor.Api.CallItemsApi("/api/So/SaleReturn/GetSaleOutWarehouses");
        AllSaleOutWarehouses.Insert(0, new SelectedItem { Text = WtmBlazor.Localizer["Sys.PleaseSelect"], Value = "" });
        AllSales = await WtmBlazor.Api.CallItemsApi("/api/So/SaleReturn/GetSales");
        AllSales.Insert(0, new SelectedItem { Text = WtmBlazor.Localizer["Sys.PleaseSelect"], Value = "" });
        AllCustomers = await WtmBlazor.Api.CallItemsApi("/api/So/SaleReturn/GetCustomers");
        AllCustomers.Insert(0, new SelectedItem { Text = WtmBlazor.Localizer["Sys.PleaseSelect"], Value = "" });
        AllMakeUsers = await WtmBlazor.Api.CallItemsApi("/api/So/SaleReturn/GetFrameworkUsers");
        AllMakeUsers.Insert(0, new SelectedItem { Text = WtmBlazor.Localizer["Sys.PleaseSelect"], Value = "" });
        if (id != "")
        {
            var rv = await WtmBlazor.Api.CallAPI<SaleReturnVM>($"/api/So/SaleReturn/{id}");
            Model = rv.Data;
        }
        SaleReturnDetailSaleReturnList3 = Model.Entity.SaleReturnDetail_SaleReturn;
        var Items = Model.Entity.SaleReturnDetail_SaleReturn;
        sumReturnNumber = Items.Any() ? Items.Sum(x => x.ReturnNumber)?.ToString("#0.####") : "0";
        sumTotalPrice = Items.Any() ? Items.Sum(x => x.TotalPrice)?.ToString("#0.####") : "0";
        sumDiscountPrice = Items.Any() ? Items.Sum(x => x.DiscountPrice)?.ToString("#0.####") : "0";
        sumTaxPrice = Items.Any() ? Items.Sum(x => x.TaxPrice)?.ToString("#0.####") : "0";
        sumTotalTaxPrice = Items.Any() ? Items.Sum(x => x.TotalTaxPrice)?.ToString("#0.####") : "0";
        await base.OnInitializedAsync();
    }
}
